<!--
 * @Author: your name
 * @Date: 2021-01-07 14:02:36
 * @LastEditTime: 2021-01-26 19:39:25
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\test\deviceTest\deviceTest.vue
-->
<template>
    <div class="test-deviceTest-main-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('devWifi.deviceSN')}}</span>
                <el-input v-big v-model="inverterSN" clearable></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('salePage.wifiSN')}}</span>
                <el-input v-model="wifiSN" clearable></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('test.test_house_type')}}</span>
                <el-select v-model="houseType" filterable :placeholder="$t('pleaseSelect')" :clearable='true'>
                    <el-option
                    v-for="item in dictCodes"
                    :key="item.value"
                    :label="item.code"
                    :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('test.test_house_name')}}</span>
                <el-input v-model="houseName" clearable></el-input>
            </div>
            <div class="search-box-sub">
                <el-button type="primary" @click='query' >{{$t('query')}}</el-button>
            </div>
        </div>
        <div class="button-box">
            <el-button type="primary"  icon='el-icon-plus' class="marginR10" @click='addMoadl'>{{$t('add')}}</el-button>
            <el-popconfirm
                :title="$t('promotionManagement.allDelete')"
                @confirm='deleteData()'
                :confirm-button-text='$t("sure")'
                :cancel-button-text='$t("cancel")'
                style="margin-right:20px;"
            >
                <el-button type="danger" slot="reference" icon='el-icon-delete' >{{$t('delete')}}</el-button>
            </el-popconfirm>
            <el-popconfirm
                :title="$t('test.deviceTest.js_Confirm_empty_test_equipment')"
                @confirm='clearData()'
                :confirm-button-text='$t("sure")'
                :cancel-button-text='$t("cancel")'
                style="margin-right:20px;"
            >
                <el-button type="danger" slot="reference" icon='el-icon-delete' >{{$t('test.deviceTest.empty')}}</el-button>
            </el-popconfirm>
            <el-button type="primary"  icon='el-icon-success' class="marginR10" @click='deviceTestFinishBatch()'>{{$t('test.deviceTest.complete')}}</el-button>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe @selection-change="handleSelectionChange($event)"  :column='column' :select='true' :row-class-name="tableRowClassName">
            <template #houseType="{data}">
              {{comm.returnCode(dictCodes,data.houseType,'value','code')}}
            </template>
            <template #continueTime="{data}">
              {{continueFormatter(data)}}
            </template>
            <template #alarmCount="{data}">
              {{alarmFormatter(data)}}
            </template>
            <template #iccid="{data}">
              {{data.deviceType == 3?data.iccid:'-'}}
            </template>
            <template #option="{data}">
                <div class="option-box">
                    <el-popconfirm
                    :title="$t('index.delete_item_confirm')"
                    @confirm='deviceTestDelete(data.wifiSN)'
                    :confirm-button-text='$t("sure")'
                    :cancel-button-text='$t("cancel")'
                    style="margin-right:10px;"
                    >
                        <el-button slot="reference" type="danger" icon='el-icon-delete' :title="$t('delete')"></el-button>
                    </el-popconfirm>
                    <el-button   type="danger" icon='el-icon-success' :title="$t('test.deviceTest.js_Complete_aging')" @click='deviceTestFinish(data.wifiSN)'></el-button>
                
                    <el-button  type="danger" icon='el-icon-zoom-in' :title="$t('js_detail')" @click='showHistoryDetail(data.wifiSN,data.inverterSN)'></el-button>
                </div>
            </template>
            
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
        <add :dictCodes='dictCodes' @uploadData='query'></add>
    </div>
</template>

<script>
import add from './add.vue'
import baseTable from '../../../components/common/baseTable'
export default {
    components: {
        baseTable,
        add
    },
    props: {

    },
    data() {
        return {
            dictCodes:[],
            inverterSN:'',
            wifiSN:'',
            houseType:'',
            houseName:'',
            loading:false,
            dataRows:[],
            column:[{
                title: this.$t('warrantyStatistics.inverterSn'),
                value: 'inverterSN',
            },
            {
                title: this.$t('servicechange.wifiSN'),
                value: 'wifiSN',
                slot: 'wifiSN',
            },
            {
                title: this.$t('test.test_house_name'),
                value: 'houseName',
                slot: 'houseName',
            },{
                title: this.$t('test.test_house_type'),
                value: 'houseType',
                slot: 'houseType',
            },
            {
                title: this.$t('test.deviceTest.wifiVersion'),
                value: 'wifiVersion',
                slot: 'wifiVersion',
            },
            {
                title: this.$t('card.iccid'),
                value: 'iccid',
                slot: 'iccid',
            },{
                title: this.$t('index.startTime'),
                value: 'beginTime',
                slot: 'beginTime',
            },
            {
                title: this.$t('device.inverter.updateTime'),
                value: 'reflashTime',
                slot: 'reflashTime',
            },
            {
                title: this.$t('test.deviceTest.continueTime'),
                value: 'continueTime',
                slot: 'continueTime',
            },
            {
                title: this.$t('test.deviceTest.alarmCount'),
                value: 'alarmCount',
                slot: 'alarmCount',
            },
            {
                title: this.$t('test.deviceTest.todayYield'),
                value: 'todayYield',
                slot: 'todayYield',
            },{
                title: this.$t('test.deviceTest.totalYield'),
                value: 'totalYield',
            },
            {
                title: this.$t('test.deviceTest.gridPower'),
                value: 'gridPower',
                slot: 'gridPower',
            },
            {
                title: this.$t('test.deviceTest.iac1'),
                value: 'iac1',
                slot: 'iac1',
            },
            {
                title: this.$t('test.deviceTest.vac1'),
                value: 'vac1',
                slot: 'vac1',
            },{
                title: this.$t('control'),
                slot: 'option',
            }],
            selectDataList:[],
            size:10,
            current:1,
            total:0
        };
    },
    computed: {

    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.deviceTestGetPage()
    },
    methods: {
        tableRowClassName({row, rowIndex}) {
            if (row.status == 1) {
                return 'error-row'
            } else if (row.status == 2) {
                return 'warning-row'
            }else{
                return ''
            }
        },
        async clearData(){
            let res = await this.API.deviceTestClear()
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        async deleteData() {
            let flag = true
            let checks = this.selectDataList
            checks.forEach(item => {
                    if (item.gridPower) {
                        flag = false
                    }
            })
            if(!(checks.length > 0)){
                this.comm.showMsg(false, '请勾选需要删除的数据！')
                return 
            }
            if (flag) {
                let res = await this.API.deviceTestDeleteBatch({
                    ids:checks.map(item =>{ return item.ids})
                })
                if(this.comm.showMsg(res.success)){
                    this.deviceTestGetPage()
                }
            } else {
                this.comm.showMsg(false, '请老化完成后再进行删除操作！')
            }
        },
        showHistoryDetail(wifiSN,inverterSN){
            this.$router.push({path:'frame-test',query:{wifiSN:wifiSN,inverterSN:inverterSN,oldPath:'/deviceTest'}})
        },
        async deviceTestFinish(wifiSN){
            let res = await this.API.deviceTestFinish({
                wifiSns:wifiSN
            })
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        async deviceTestFinishBatch(){
            if(this.selectDataList.length == 0){
                this.comm.showMsg(false,this.$t('test.deviceTest.js_Please_choose_to_complete_the_aging_equipment'))
            }
            let res = await this.API.deviceTestFinish({
                wifiSns:this.selectDataList.map(item =>{
                    return item.wifiSN
                })
            })
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        async deviceTestDelete(wifiSN){
            let res = await this.API.deviceTestDelete({
                wifiSN:wifiSN
            })
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        alarmFormatter(row){
            var inverterAlarm=row.inverterAlarmNum==null?0:row.inverterAlarmNum;
            var batteryAlarm=row.batteryAlarmNum==null?0:row.batteryAlarmNum;
            return inverterAlarm+"+"+batteryAlarm;
        },
        continueFormatter(row){
            var beginTime = Date.parse(new Date(row.beginTime.replace(/-/g, "/"))); 
            var currentTime=new Date();
            var date3=currentTime-beginTime;
            
            var days=Math.floor(date3/(24*3600*1000));
            
            //计算出小时数

            var leave1=date3%(24*3600*1000);    //计算天数后剩余的毫秒数
            var hours=Math.floor(leave1/(3600*1000));
            //计算相差分钟数
            var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
            var minutes=Math.floor(leave2/(60*1000));
            //计算相差秒数
            var leave3=leave2%(60*1000);      //计算分钟数后剩余的毫秒数
            var seconds=Math.round(leave3/1000);
            var totalHours=days*24+hours;
            return totalHours+' '+this.$t('test.deviceTest.testConfig.hour')+' '+minutes+' '+this.$t('test.deviceTest.js_deleteTestDevice_minutes')+' '+seconds+' '+this.$t('test.deviceTest.js_deleteTestDevice_second');
        },
        async deviceTestGetPage(){
            this.loading = true
            let res = await this.API.deviceTestGetPage({
                inverterSN:this.inverterSN,
                wifiSN:this.wifiSN,
                houseType:this.houseType,
                houseName:this.houseName,
                size:this.size,
                current:this.current,
            })
            this.dataRows = res.rows
            this.total = res.total
            this.loading = false
        },
        addMoadl(){
            this.$store.commit('setNewVisible',true)
        },
        saleDeleteBatch(){

        },
        query(){
            this.current = 1
            this.deviceTestGetPage()
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:`TEST_HOUSE_TYPE`,
                lang:localStorage.getItem('lang') || 'zh_CN'
            })
            this.dictCodes = res.TEST_HOUSE_TYPE
        },
        handleSelectionChange(data){
            this.selectDataList = data
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.deviceTestGetPage()
        },
        handleCurrentChange(val) {
          this.current = val
          this.deviceTestGetPage()
        },
    },
};
</script>

<style  lang="less">
.test-deviceTest-main-box{
    .error-row {
        td{
            background: red !important;
        }
    }
    .warning-row {
        td{
            background: orange !important;
        }
    }
}
</style>
